package com.haiyi.activiti.engine.framework.activiti.manager.impl;

import com.haiyi.activiti.engine.framework.activiti.manager.TaskManager;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.DelegationState;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p> </p>
 *
 * <pre> Created: 2018/12/5 17:21 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class TaskManagerImpl implements TaskManager {
    @Autowired
    private TaskService taskService;

    @Override
    public Task getProcessCurrentTask(String processInstanceId) {
        return taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
    }

    @Override
    public List<Task> getAssignTasks(String userId) {
        return taskService.createTaskQuery().taskAssignee(userId).list();
    }

    @Override
    public List<Task> getCandidateTasks(String userId) {
        return taskService.createTaskQuery().taskCandidateUser(userId).list();
    }

    @Override
    public List<Task> getCandidateOrAssigned(String userId) {
        return taskService.createTaskQuery().taskCandidateOrAssigned(userId).list();
    }

    @Override
    public void claimTask(String taskId, String userId) {
        taskService.claim(taskId,userId);
    }

    @Override
    public void unClaimTask(String taskId) {
        taskService.unclaim(taskId);
    }

    @Override
    public void delegateTask(String taskId, String delegateUserId) {
        taskService.delegateTask(taskId,delegateUserId);
    }

    @Override
    public List<Task> getDelegateTasks(String userId) {
        return taskService.createTaskQuery().taskAssignee(userId).taskDelegationState(DelegationState.PENDING).list();
    }

    @Override
    public void compleDelegateTask(String taskId, Map<String,Object> map) {
        taskService.resolveTask(taskId,map);
    }

    @Override
    public List<Task> getResolveDelegateTasks(String userId) {
        return taskService.createTaskQuery().taskAssignee(userId).taskDelegationState(DelegationState.RESOLVED).list();
    }

    @Override
    public void completeTask(String taskId,Map<String,Object> variables) {
        taskService.complete(taskId,variables);
    }

    @Override
    public Map<String, Object> getParams(String taskId) {
        return taskService.getVariables(taskId);
    }


}
